<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\MenuModel;
use app\admin\service\MenuService;
use app\Request;
use think\facade\View;

class Menu extends AdminController
{
    /**
     * 菜单列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getMenuJson'){
            $data = MenuService::getMenuList();
            return json([
                'code' => 0,
                'msg' => '',
                'count' => count($data),
                'data' => $data,
            ]);
        }

        return view::fetch('index');
    }

    /**
     * 菜单添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->post();
            $result = MenuService::menuAddOrEdit($params);
            return json($result);
        }
        //获取后台菜单
        $menuList = MenuService::getMenuList();
        return view::fetch('add',[
            'menuList' => $menuList,
        ]);
    }

    /**
     * 菜单编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->post();
            $result = MenuService::menuAddOrEdit($params);
            return json($result);
        }
        //获取后台菜单
        $menuList = MenuService::getMenuList();
        $menuInfo = MenuModel::where('id',$request->get('id'))->find();
        return view::fetch('edit',[
            'menuList' => $menuList,
            'menuInfo' => $menuInfo,
        ]);
    }

    /**
     * 删除菜单
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $id = $request->param('id',0);//菜单ID
        $res = MenuService::menuDelete($id);
        return json($res);
    }
}
